package com.bit.book.controller;

import com.bit.book.constant.Constants;
import com.bit.book.entity.BookInfo;
import com.bit.book.entity.PageRequest;
import com.bit.book.entity.PageResult;
import com.bit.book.entity.Result;
import com.bit.book.enums.ResultCode;
import com.bit.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        List<BookInfo> bookInfos = bookService.getList();
        return bookInfos;
    }

    @RequestMapping(value = "/addBook", produces = "application/json;charset=utf-8")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, bookInfo: {}", bookInfo);
        //1. 参数校验
        //2. 添加图书
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() ==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() ==null
        ){
            return "参数不合法, 请重新检查";
        }
        Integer result = bookService.addBook(bookInfo);

        return result==1?"":"图书插入失败";
    }
    @RequestMapping("/getListByPage")
    public Result<PageResult> getListByPage(PageRequest pageRequest){
        log.info("获取图书列表, pageRequest:{}", pageRequest);
        PageResult pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("查询图书详情, bookId:"+ bookId);
        if (bookId==null || bookId<=0){
            return null;
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookInfo;
    }

    @RequestMapping(value = "/updateBook", produces = "application/json;charset=utf-8")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书, bookInfo:{}", bookInfo);
        //参数校验
        if (bookInfo.getId()<=0){
            return "参数不正确, 请重新检查";
        }
        Integer result = bookService.updateBookById(bookInfo);
        return result ==1? "": "图书更新失败";
    }

    @RequestMapping(value = "/deleteBook", produces = "application/json;charset=utf-8")
    public String deleteBook(Integer bookId){
        log.info("删除图书, bookId:{}", bookId);
        //参数校验
        if (bookId<=0){
            return "参数不正确, 请重新检查";
        }
        Integer result = bookService.deleteBook(bookId);
        return result ==1? "": "图书删除失败";
    }

    @RequestMapping(value = "/batchDeleteBook", produces = "application/json;charset=utf-8")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids: {}", ids);
        try {
            bookService.batchDeleteBook(ids);
            return "";
        }catch (Exception e){
            log.error("批量删除失败, e:", e);
            return "批量删除失败";
        }
    }

}
